﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Perceiveit.Xml
{
    public class XPropertyListBag : IXPropertyBag
    {
        private class XPropertyCombined
        {
            public XProperty Property;
            public XPropertyCell Cell;
        }

        private List<XPropertyCombined> _all = new List<XPropertyCombined>();

        public int Count
        {
            get { return _all.Count; }
        }

        public bool TryGetCell(XProperty property, out XPropertyCell cell)
        {
            foreach (XPropertyCombined comb in this._all)
            {
                if (comb.Property == property)
                {
                    cell = comb.Cell;
                    return true;
                }
            }
            cell = null;
            return false;
        }

        public void SetCell(XProperty property, XPropertyCell cell)
        {
            foreach (XPropertyCombined comb in this._all)
            {
                if (comb.Property == property)
                {
                    comb.Cell = cell;
                    return;
                }
            }
            XPropertyCombined newcomb = new XPropertyCombined();
            newcomb.Property = property;
            newcomb.Cell = cell;

            _all.Add(newcomb);
        }

        public void ClearCell(XProperty property)
        {
            int index = 0;
            bool found = false;
            foreach (XPropertyCombined comb in this._all)
            {
                if (comb.Property == property)
                {
                    found = true;
                    break;
                }
                index++;
            }

            if (found)
                _all.RemoveAt(index);
        }
    }
}
